package com.lgl.scs.system.interceptor;

import com.lgl.scsapi.util.utils.Constants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Shiro登录验证器
 */
public class LoginHandlerInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String path = request.getServletPath();
		if(path.matches(Constants.NO_INTERCEPTOR_PATH)){
			return true;
		}
		//shiro管理的session
		Session session = SecurityUtils.getSubject().getSession();
		Object appUserObj = session.getAttribute(Constants.SESSION_USER);
		if (appUserObj != null) {
			return true;
		}

		//登陆过滤
		response.sendRedirect(request.getContextPath());
		return true;
	}
}